/*
 * Copyright 2007-2010 Jeremy Brown.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author <a href="http://jeremy-brown.com">Jeremy Brown</a>
 */


package groovy.prefuse.factory

import prefuse.data.Graph
import prefuse.data.Tree
import prefuse.data.Node as PrefuseNode //so we don't conflict with groovy.util.Node
import prefuse.data.Edge

public class EdgeFactory extends AbstractFactory
{
	boolean isLeaf()
	{
		return false
	}
    
    public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException
	{
		def parentGraph = builder.getCurrent() //get this node's parent node - getParentNode() returns grandparent node when used in the newInstance(...) method
		
		def values = []
		boolean listToListEdges = false
		
		if(attributes.source && attributes.target)
		{
			//source and target can be used to specify directionality of edges in a Graph
			if(attributes.source instanceof List || attributes.target instanceof List)
			{
				//Allow for one-to-many, many-to-one, or many-to-many edge definitions.
				if(!(attributes.source instanceof List))
				{
					attributes.source = [attributes.source]
				}
				if(!(attributes.target instanceof List))
				{
					attributes.target = [attributes.target]
				}
				listToListEdges = true
			}
			else
			{
				//pfEdge(source:nodeA, target:nodeB) //this will make an edge from nodeA to nodeB
				values << attributes.remove("source")
				values << attributes.remove("target")
			}
		}
		else if(value instanceof List)
		{
			//a list of nodes can be used to specify a linked chain of 2+ nodes. The source node will always be node N in the list and the target node will be node N+1 in the list
			//edge([nodeA, nodeB]) //creates edge A-B
			//edge([nodeA, nodeB, nodeE, nodeD, nodeC]) //creates edges A-B, B-E, E-D, and D-C
			values = value
		}
		
		
		def edges = []
		if(listToListEdges)
		{
			//Make an edge from every node in the source list to every node in the target list
			attributes.source.each{sourceNode->
				attributes.target.each{targetNode->
					Edge edge = parentGraph.addEdge(sourceNode, targetNode)
					edges << edge
				}
			}
			
			//Remove these attributes now that we're done with them, otherwise they'll cause problems in the PrefuseBuilder later.
			attributes.remove("source")
			attributes.remove("target")
		}
		else
		{
			//Create an edge from each item in the list to next item in the list, in a chain.
			//If nodes named A, B, C, and D are passed in as [A, B, C, D], this should create edges A-B, B-C, and C-D
			def sourceNode = null
			if(values instanceof List)
			{
				values.each{targetNode->
					if(sourceNode && targetNode)
					{
						Edge edge = parentGraph.addEdge(sourceNode, targetNode)
						edges << edge
						sourceNode = targetNode
					}
					else
					{
						sourceNode = targetNode
					}
				}
			}
		}
		
		return edges
	}
}
